#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "monitorwindow.h"
#include "databasewindow.h"
#include <QMessageBox>
#include <QCloseEvent>
#include <QApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , monitorWindow(nullptr)
    , databaseWindow(nullptr)
{
    ui->setupUi(this);
    
    // 设置窗口属性
    setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
    
    // 设置窗口居中
    resize(600, 400);
    
    setupConnections();
}

MainWindow::~MainWindow()
{
    // 关闭独立窗口
    if (monitorWindow) {
        monitorWindow->close();
        monitorWindow = nullptr;
    }
    if (databaseWindow) {
        databaseWindow->close();
        databaseWindow = nullptr;
    }
    delete ui;
}

void MainWindow::setupConnections()
{
    connect(ui->monitorButton, &QPushButton::clicked, this, &MainWindow::onMonitorButtonClicked);
    connect(ui->databaseButton, &QPushButton::clicked, this, &MainWindow::onDatabaseButtonClicked);
    connect(ui->logoutButton, &QPushButton::clicked, this, &MainWindow::onLogoutButtonClicked);
}

void MainWindow::onMonitorButtonClicked()
{
    if (!monitorWindow) {
        monitorWindow = new MonitorWindow(nullptr);  // 创建为独立窗口
        connect(monitorWindow, &MonitorWindow::windowClosed, this, &MainWindow::onChildWindowClosed);
    }
    
    // 隐藏主窗口
    this->hide();
    
    monitorWindow->show();
    monitorWindow->raise();
    monitorWindow->activateWindow();
}

void MainWindow::onDatabaseButtonClicked()
{
    if (!databaseWindow) {
        databaseWindow = new DatabaseWindow(nullptr);  // 创建为独立窗口
        connect(databaseWindow, &DatabaseWindow::windowClosed, this, &MainWindow::onChildWindowClosed);
    }
    
    // 隐藏主窗口
    this->hide();
    
    databaseWindow->show();
    databaseWindow->raise();
    databaseWindow->activateWindow();
}

void MainWindow::onLogoutButtonClicked()
{
    int ret = QMessageBox::question(this, "确认退出", "确定要退出登录吗？",
                                   QMessageBox::Yes | QMessageBox::No,
                                   QMessageBox::No);
    
    if (ret == QMessageBox::Yes) {
        // 关闭所有子窗口
        if (monitorWindow) {
            monitorWindow->close();
        }
        if (databaseWindow) {
            databaseWindow->close();
        }
        
        // 隐藏主窗口并显示登录窗口
        this->hide();
        
        // 查找登录窗口并显示
        foreach (QWidget *widget, QApplication::topLevelWidgets()) {
            if (widget->objectName() == "LoginWindow" || widget->inherits("LoginWindow")) {
                widget->show();
                widget->raise();
                widget->activateWindow();
                break;
            }
        }
    }
}

void MainWindow::onChildWindowClosed()
{
    // 子窗口关闭时的处理
    QObject *sender = this->sender();
    if (sender == monitorWindow) {
        monitorWindow = nullptr;
    } else if (sender == databaseWindow) {
        databaseWindow = nullptr;
    }
    
    // 重新显示主窗口
    this->show();
    this->raise();
    this->activateWindow();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    int ret = QMessageBox::question(this, "确认退出", "确定要关闭程序吗？",
                                   QMessageBox::Yes | QMessageBox::No,
                                   QMessageBox::No);
    
    if (ret == QMessageBox::Yes) {
        // 关闭所有子窗口
        if (monitorWindow) {
            monitorWindow->close();
        }
        if (databaseWindow) {
            databaseWindow->close();
        }
        
        event->accept();
        QApplication::quit();
    } else {
        event->ignore();
    }
}